/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package racer.client.gui;

import racer.client.gui.entities.Entity;
import racer.client.gui.entities.CarEntity;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferStrategy;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author Piet
 */
public class Game extends Canvas {

    private ArrayList<Entity> entities = new ArrayList<Entity>();
    private Entity car;
    private Double moveSpeed = 300.0;
    private Boolean gameRunning = true;
    private boolean leftPressed = false;
    private boolean rightPressed = false;
    private boolean upPressed = false;
    private boolean downPressed = false;
    // de strategie die gebruikt moet worden om het veranderen van schermen te versnellen
    private BufferStrategy strategy;

    public Game() {
        JFrame container = new JFrame("Racer");

        JPanel panel = (JPanel) container.getContentPane();
        panel.setPreferredSize(new Dimension(800, 600));
        panel.setLayout(null);

        setBounds(0, 0, 800, 600);
        panel.add(this);

        // AWT moet het scherm niet hertekenen, dit doen we zelf
        setIgnoreRepaint(true);
        
        container.pack();
        container.setResizable(false);
        container.setVisible(true);

        container.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        addKeyListener(new InputHandler());

        requestFocus();

        //buffer strategy die AWT toelaat om de graphics te versnellen
        createBufferStrategy(2);
        strategy = getBufferStrategy();

        initEntities();
    }

    private void gameLoop() {
        long lastLoopTime = System.currentTimeMillis();
        while (gameRunning){
            long delta = System.currentTimeMillis() - lastLoopTime;
            lastLoopTime = System.currentTimeMillis();
            
            Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
            g.setColor(Color.WHITE);
            g.fillRect(0, 0, 800, 600);
            
            for (int i = 0; i<entities.size(); i++){
                Entity entity = (Entity) entities.get(i);
                entity.move(delta);
            }
            
            for (int i = 0; i < entities.size(); i++) {
                Entity entity = (Entity) entities.get(i);
                entity.draw(g);
            }
            
            g.dispose();
            strategy.show();
            
            car.setHorizontalMovement(0);
            if ((leftPressed) && (!rightPressed)) {
                car.setHorizontalMovement(-moveSpeed);
            } else if ((rightPressed) && (!leftPressed)) {
                car.setHorizontalMovement(moveSpeed);
            }	
            
            car.setVerticalMovement(0);
            if (upPressed && !downPressed) {
                car.setVerticalMovement(-moveSpeed);
            } else if (downPressed && !upPressed){
                car.setVerticalMovement(moveSpeed);
            }

            try { Thread.sleep(10); } catch (Exception e) {}
        }
    }
    
    private void initEntities() {
	// create the player ship and place it roughly in the center of the screen
	car = new CarEntity(this,400,300);
	entities.add(car);
    }

    private class InputHandler extends KeyAdapter {

        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                leftPressed = true;
            }
            if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                rightPressed = true;
            }
            if (e.getKeyCode() == KeyEvent.VK_UP) {
                upPressed = true;
            }
            if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                downPressed = true;
            }
        }

        @Override
        public void keyReleased(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                leftPressed = false;
            }
            if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                rightPressed = false;
            }
            if (e.getKeyCode() == KeyEvent.VK_UP) {
                upPressed = false;
            }
            if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                downPressed = false;
            }
        }

        @Override
        public void keyTyped(KeyEvent e) {
            if (e.getKeyChar() == 27) {
                System.exit(0);
            }
        }
    }

    public static void main(String[] args) {
        Game g = new Game();
        g.gameLoop();
        
    }
}
